package com.radiant.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.UriBuilder;

import com.google.inject.Inject;
import com.radiant.data.entity.Child;
import com.radiant.data.entity.ChildDAO;
import com.radiant.data.entity.Profile;
import com.radiant.data.entity.ProfileDAO;
import com.radiant.data.entity.Sex;
import com.radiant.data.entity.User;
import com.radiant.data.entity.UserDAO;

@Path("/user")
public class UserServiceImpl implements UserService {

	@Inject
	UserDAO userDAO;

	@Inject
	ProfileDAO profileDAO;

	@Inject
	ChildDAO childDAO;

	@Inject
	UserAuthenticationService authService;

	@Inject
	MonthlyTrackerService trackerService;

	private static final ThreadLocal<User> currentUser = new ThreadLocal<User>();
	
	public UserServiceImpl() {
		super();
	}

	public List<String> getProtectedPaths() {
		List<String> paths = new ArrayList<String>();
		UriBuilder builder = UriBuilder.fromResource(this.getClass());
		paths.add(builder.path(getClass(), "addChild").build().toString());
		builder = UriBuilder.fromResource(this.getClass());
		paths.add(builder.path(getClass(), "addFriend").build().toString());
		builder = UriBuilder.fromResource(this.getClass());
		paths.add(builder.path(getClass(), "updateProfile").build().toString());
		return paths;
	}

	public void setCurrentUser(User user) {
		currentUser.set(user);
	}
	
	public User getCurrentUser() {
		return currentUser.get();
	}

	@POST @Path("/new")
	@Consumes("application/x-www-form-urlencoded")
	@Produces("application/json")
	public User createNewUser(@FormParam("userName") String userName, @FormParam("password") String password) {
		User user = new User();
		user.setUserName(userName);
		user.setPassword(authService.generateEncryptedPassword(password));
		user = userDAO.save(user);
		return user;
	}

	@POST @Path("/addChild")
	@Consumes("application/x-www-form-urlencoded")
	@Produces("application/json")
	public void addChild(@FormParam("childName") String childName, 
			@FormParam("dateOfBirth") Date dateOfBirth, 
			@FormParam("sex") Sex sex) {
		User user = getCurrentUser();
		List<Child> listChildren = user.getChildren();
		Child child = new Child();
		child.setName(childName);
		child.setDateOfBirth(dateOfBirth);
		child.setSex(sex);
		
		child = childDAO.save(child);
		trackerService.createDefaultDevelopmentTracks(child.getChildId());
		
		listChildren.add(child);
		user.setChildren(listChildren);

		userDAO.save(user);
	}
	
	@POST @Path("/addFriend")
	@Consumes("application/x-www-form-urlencoded")
	@Produces("application/json")
	public void addFriend(@FormParam("friendId") String friendId) {
		User user = getCurrentUser();
		List<User> listFriends = user.getFriends();
		User friend = userDAO.findByPrimaryKey(friendId, true);
		listFriends.add(friend);
		user.setFriends(listFriends);
		userDAO.save(user);
	}

	@POST @Path("/updateProfile")
	@Consumes("application/x-www-form-urlencoded")
	@Produces("application/json")
	public void updateProfile(@FormParam("email") String email, 
			@FormParam("dateOfBirth") Date dateOfBirth,
			@FormParam("phoneNumber") String phoneNumber, 
			@FormParam("sex") Sex sex) {
		User user = getCurrentUser();
		Profile profile = user.getProfile();
		if (profile == null) {
			profile = new Profile();
		}
		profile.setEmail(email);
		profile.setDateOfBirth(dateOfBirth);
		profile.setPhoneNumber(phoneNumber);
		profile.setSex(sex);

		profileDAO.save(profile);
	}

}
